package com.share.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.device.domain.Region;
import com.share.device.mapper.RegionMapper;
import com.share.device.service.IRegionService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.swing.*;
import java.util.Collections;
import java.util.List;

@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region>  implements IRegionService{
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public List<Region> treeSelect(String parentCode) {
        val regionLambdaQueryWrapper = new LambdaQueryWrapper<Region>();
        val eq = regionLambdaQueryWrapper.eq(Region::getParentCode, parentCode);
        val regionList = regionMapper.selectList(eq);
        if(!CollectionUtils.isEmpty(regionList)){
            for (var region : regionList) {
                val wrapper = new LambdaQueryWrapper<Region>();
                val eq1 = wrapper.eq(Region::getParentCode, region.getCode());
                val l = regionMapper.selectCount(eq1);
                region.setHasChildren(l > 0);

            }
        }
        return regionList;
    }

    @Override
    public String getNameByCode(String provinceCode) {
        if("".equals(provinceCode)){
            return  "";
        }
        val regionLambdaQueryWrapper = new LambdaQueryWrapper<Region>();
        val wrapper = regionLambdaQueryWrapper.eq(Region::getCode, provinceCode);
        Region region = regionMapper.selectOne(wrapper);
        if(region == null){
            return "";
        }
        return region.getName();
    }
}